<?php
/**
 * Created by PhpStorm.
 * User: work
 * Date: 18-8-4
 * Time: 下午6:34
 */

namespace PddWechat\Models\User\Service;


use Dai\Framework\Base\BaseSessionInfo;
use PddWechat\Models\User\Data\UserData;
use PddWechat\Models\User\Data\UserInviteRecordData;

class InviteList
{
    public function execute($basePageInfo)
    {
        $res = [
            'total' => 0,
            'list' => [],
            'title' => '推荐好友列表',
        ];
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;

        $userInviteRecordData = new UserInviteRecordData();
        $records = $userInviteRecordData ->selectOpenIdInviteRecords($sessionData->openId);
       // $records = $userInviteRecordData ->selectOpenIdInviteRecords("ofulZ02VkshM8a5Jm-4fFIg9H5QA");
        if( count($records) == 0) {
            return $res;
        }

        $openIds = [];
        if( count($records) > 0 ){
            foreach ($records as $record) {
                $openIds[] = $record->openId;
            }
        }
        $userData = new UserData();
        $userList = $userData->getUserByOpenIds($openIds);

        foreach ($records as $record) {
            if( isset($userList[$record->openId]) ) {
                $record->userName = $userList[$record->openId]->userName;
                $record->statusName =  $record->status > 0 ? "成功" : "非新用户";
                $res['list'][] = $record;
            }
        }
        $res['total'] = count($res['list']);
        return $res;
    }
}